#!/bin/sh
if [ ! -f /initialized ]; then {  #是否初始化,初始化了就不需要了
	touch /initialized
	if [ $STORAGE_PORT ]; then	 #storage端口
		sed -i "s#\(^port\).*#\1=$STORAGE_PORT#" /etc/fdfs/storage.conf 
		sed -i "s#\(^storage_server_port\).*#\1=$STORAGE_PORT#" /etc/fdfs/mod_fastdfs.conf
	else
		STORAGE_PORT=23000
	fi
	if [ $GROUP_NAME ]; then	#组名 
		sed -i "s#\(^group_name\).*#\1=$GROUP_NAME#" /etc/fdfs/storage.conf
		sed -i "s#\(^group_name\).*#\1=$GROUP_NAME#" /etc/fdfs/mod_fastdfs.conf
	fi
	if [ $STORAGE_BASE_PATH ]; then	 #存储日志文件的基本路径
		mkdir -p $STORAGE_BASE_PATH
		sed -i "s#\(^base_path\).*#\1=$STORAGE_BASE_PATH#" /etc/fdfs/storage.conf
		sed -i "s#\(^base_path\).*#\1=$STORAGE_BASE_PATH#" /etc/fdfs/mod_fastdfs.conf
	fi
	if [ $STORAGE_PATH0 ]; then		#存储文件路径
		sed -i "s#\(^store_path0\).*#\1=$STORAGE_PATH0#" /etc/fdfs/storage.conf
		sed -i "s#\(^store_path0\).*#\1=$STORAGE_PATH0#" /etc/fdfs/mod_fastdfs.conf
	else
		STORAGE_PATH0=/home/yuqing/fastdfs
	fi
	#tracker 服务器地址
	if [ $TRACKER_SERVER ]; then #清除原来的配置
		sed -i "s#\(^tracker_server\).*##" /etc/fdfs/storage.conf 
		sed -i "s#\(^tracker_server\).*##" /etc/fdfs/mod_fastdfs.conf
	fi
	if [ $TRACKER_SERVER ]; then	
		for i in $( echo $TRACKER_SERVER | awk -v FS="," '{for(i=1;i<=NF;i++)print $i}'  );do echo "tracker_server=$i" >> /etc/fdfs/storage.conf;done #循环配置tracker
		for i in $( echo $TRACKER_SERVER | awk -v FS="," '{for(i=1;i<=NF;i++)print $i}'  );do echo "tracker_server=$i" >> /etc/fdfs/mod_fastdfs.conf;done
	fi
	if [ $HTTP_SERVER_PORT ]; then	
		sed -i "s#\(^http.server_port\).*#\1=$HTTP_SERVER_PORT#" /etc/fdfs/storage.conf
		sed -i "s#\(^http.server_port\).*#\1=$HTTP_SERVER_PORT#" /etc/fdfs/mod_fastdfs.conf
		sed -i "s#listen       8080#listen       $HTTP_SERVER_PORT#g" /usr/local/nginx/conf/nginx.conf
	fi
	#是否加组名 true:goup1/M00/00/00/xxx  false:/M00/00/00/xxx
	if [ $URL_HAVE_GROUP_NAME ]; then	
		sed -i "s#\(^url_have_group_name\).*#\1=$URL_HAVE_GROUP_NAME#" /etc/fdfs/mod_fastdfs.conf
	fi
	#组数量
	if [ $GROUP_COUNT ]; then	
		sed -i "s#\(^group_count\).*#\1=$GROUP_COUNT#" /etc/fdfs/mod_fastdfs.conf
	fi
	if [ $GROUP_COUNT ]; then	
		#add groups
		i=1
		while(( $i<=$GROUP_COUNT ))
		do
			echo "[group$i]" >> /etc/fdfs/mod_fastdfs.conf
			echo "group_name=group$i" >> /etc/fdfs/mod_fastdfs.conf
			echo "storage_server_port=$STORAGE_PORT" >> /etc/fdfs/mod_fastdfs.conf
			echo "store_path_count=1" >> /etc/fdfs/mod_fastdfs.conf
			echo "store_path0=$STORAGE_PATH0" >> /etc/fdfs/mod_fastdfs.conf
			let "i++"
		done
	fi
}
fi
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart

cd /etc/fdfs
touch mime.types
#校验和启动nginx
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx 

if [ $STORAGE_BASE_PATH ]; then	
	tail -f $STORAGE_BASE_PATH/logs/storaged.log
else 
	tail -f /home/yuqing/fastdfs/logs/storaged.log
fi